package com.example.webbrowser.Adapter;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.TextView;

import com.example.webbrowser.Entity.MarkListView;
import com.example.webbrowser.MainActivity;
import com.example.webbrowser.R;

import java.util.List;

public class MarkAdapter extends BaseAdapter {
    List list;
    Activity activity;
    boolean running;
    public MarkAdapter(List item, Activity mainactivity) {
        this.list = item;
        this.activity = mainactivity;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = null;
        //布局不变，数据变

        //如果缓存为空，我们生成新的布局作为1个item
        if(convertView==null){
            Log.i("info:", "没有缓存，重新生成"+position);
            LayoutInflater inflater = activity.getLayoutInflater();
            //因为getView()返回的对象，adapter会自动赋给ListView
            view = inflater.inflate(R.layout.bookmark_listview, null);
        }else{
            Log.i("info:", "有缓存，不需要重新生成"+position);
            view = convertView;
        }
        MarkListView m = (MarkListView) list.get(position);
        TextView tv_id = view.findViewById(R.id.item_id);
        tv_id.setText(  m.getId()  );
        TextView tv_Name = view.findViewById(R.id.item_name);
        tv_Name.setText(  m.getName()  );
        TextView tv_url = view.findViewById(R.id.item_url);
        tv_url.setText(  m.getUrl()  );

        return view;
    }
}
